<?xml version="1.0"?>

<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:exsl="http://exslt.org/common">

	<!-- Non changing -->
	
	<xsl:variable name="Data">
		<xsl:text>Data</xsl:text>
	</xsl:variable>

	<xsl:variable name="Object">
		<xsl:text>Object</xsl:text>
	</xsl:variable>

	<xsl:variable name="Collection">
		<xsl:text>Collection</xsl:text>
	</xsl:variable>

	<xsl:template name="CollectionVariableSQLTableName">
		<xsl:param name="Class" />
		<xsl:param name="CollectionType" />

		<xsl:value-of select="$Class/@name"/>
		<xsl:text>__</xsl:text>
		<xsl:value-of select="$CollectionType/@variable"/>
		<xsl:text>__Collection</xsl:text>
	</xsl:template>

	<!-- Returns the added columns after drops and renames have been taken into account -->
	<!-- Not working yet -->
	<xsl:template name="UpToDateColumns">
		<xsl:param name="Class" />
		<xsl:copy-of select="$Class/Build"/>
	</xsl:template>

	<xsl:template name="GotListAdd">
		<xsl:param name="Element" />
		<xsl:param name="JavaType" />

		<xsl:variable name="ElementType">
			<xsl:call-template name="ElementType">
				<xsl:with-param name="Element" select="$Element" />
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="GetMethod">
			<xsl:call-template name="GetMethodUsingCursor">
				<xsl:with-param name="Type" select="Element" />
			</xsl:call-template>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$ElementType=$Data">
				gotList.add(listC.<xsl:value-of select="$GetMethod" />(_childIndex2));
			</xsl:when>
			<xsl:when test="$ElementType=$Object">
				/*This is just to get the XSL to play nice with the formatting*/
				<xsl:value-of select="$JavaType" /> newValue2 = new <xsl:value-of select="$JavaType" />();
				newValue2 = DatabaseHelper.<xsl:value-of select="$JavaType" />Helper.Query.GetById(db, listC.getLong(_childIndex2));
				gotList.add(newValue2);
			</xsl:when>
		</xsl:choose>
	</xsl:template>



	<!-- Changeable -->
	
	<xsl:template name="GetMethodUsingCursor">
		<xsl:param name="Type" />
		<xsl:choose>
			<xsl:when test="name($Type)='Date'">
				<xsl:text>getString</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='short' or name($Type)='Short'">
				<xsl:text>getShort</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='byte' or name($Type)='Byte'">
				<xsl:text>getInt</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='long' or name($Type)='Long'">
				<xsl:text>getLong</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='float' or name($Type)='Float'">
				<xsl:text>getFloat</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='boolean' or name($Type)='Boolean'">
				<xsl:text>getInt</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='char' or name($Type)='Character'">
				<xsl:text>getString</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='double' or name($Type)='Double'">
				<xsl:text>getDouble</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='int' or name($Type)='Integer'">
				<xsl:text>getInt</xsl:text>
			</xsl:when>
			<xsl:when test="name($Type)='String'">
				<xsl:text>getString</xsl:text>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="IsNullable">
		<xsl:param name="Element" />

		<xsl:choose>
			<xsl:when test="name($Element)='Date'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='short'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Short'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='byte'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Byte'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='long'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Long'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='float'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Float'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='boolean'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Boolean'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='char'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Character'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='double'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Double'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='int'">
				<xsl:number value="0" />
			</xsl:when>
			<xsl:when test="name($Element)='Integer'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='String'">
				<xsl:number value="1" />
			</xsl:when>
			<xsl:when test="name($Element)='Object'">
				<xsl:number value="1" />
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="SelectAllDataAndObjects">
		<xsl:param name="Class" />
		<xsl:copy-of 
			select="$Class/Build/Add/short | 
					$Class/Build/Add/Short | 
					$Class/Build/Add/byte | 
					$Class/Build/Add/Byte | 
					$Class/Build/Add/long | 
					$Class/Build/Add/Long | 
					$Class/Build/Add/float | 
					$Class/Build/Add/Float | 
					$Class/Build/Add/boolean | 
					$Class/Build/Add/Boolean | 
					$Class/Build/Add/char | 
					$Class/Build/Add/Character | 
					$Class/Build/Add/double | 
					$Class/Build/Add/Double | 
					$Class/Build/Add/int | 
					$Class/Build/Add/Integer | 
					$Class/Build/Add/String | 
					$Class/Build/Add/Date | 
					$Class/Build/Add/Object"/>
	</xsl:template>

	<xsl:template name="JavaTypeFromElement">
		<xsl:param name="Element" />
		<xsl:choose>
			<xsl:when test="name($Element)='Object'">
				<xsl:value-of select="@class" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="name($Element)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="SelectAllCollections">
		<xsl:param name="Class" />
		<xsl:copy-of select="$Class/Build/Add/List | $Class/Build/Add/Array"/>
	</xsl:template>

	<xsl:template name="SelectAllCollectionElements">
		<xsl:param name="Class" />
		<xsl:copy-of select="$Class/Build/Add/Array/* | $Class/Build/Add/List/*"/>
	</xsl:template>

	<xsl:template name="ElementType">
		<xsl:param name="Element" />
		
		<xsl:choose>
			<xsl:when test="name($Element)='Object'">
				<xsl:value-of select="$Object"/>
			</xsl:when>
			<xsl:when test="name($Element)='Array' or 
							name($Element)='List'">
				<xsl:value-of select="$Collection"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$Data"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="SetListAdd">
		<xsl:param name="CollectionType" />
		<xsl:param name="JavaType" />
		<xsl:choose>
			<xsl:when test="name($CollectionType)='List'">
							object.<xsl:value-of select="MethodMap/@set" />(gotList);
			</xsl:when>
			<xsl:when test="name($CollectionType)='Array'">
							object.<xsl:value-of select="MethodMap/@set" />(gotList.toArray(new <xsl:value-of select="$JavaType" />[0]));
			</xsl:when>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
